package com.plwf.lake.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.plwf.lake.entity.Student;
import com.plwf.lake.service.StudentService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 学生接口
 * @author ：plwf
 * @time：2019-5-12
 */
@RestController
@RequestMapping("/student")
public class StudentCtrl {
    @Resource
    private StudentService service;

    //分页查询，学生
    @RequestMapping("/query")
    public Map<String,Object> query(HttpServletRequest request){
        //查询条件
        String stuId = request.getParameter("stuId");
        Map<String,Object> map = new HashMap<>();
        if(stuId!=null && !stuId.equals("")){
            map.put("stuId",Integer.parseInt(stuId));
        }else{
            map.put("stuId",null);
        }
        //通过备注信息查询学员以代替通过微信号查询
        String remark = request.getParameter("remark");
        if(remark!=null && !remark.equals("")){
            map.put("remark",remark);
        }else{
            map.put("remark",null);
        }
        //设置分页条件
        int page=1;
        int rows=5;
        String p = request.getParameter("page");
        if(p!=null && !p.equals("")){
            page = Integer.parseInt(p);
        }
        String r = request.getParameter("rows");
        if(r!=null && !r.equals("")){
            rows = Integer.parseInt(r);
        }

        //分页插件的工具类 ，传入当前页数和条数，并且先调用在查询
        PageHelper.startPage(page,rows);
        //查询出来的教练信息
        List<Student> list = service.query(map);
        //获取分页信息，并且一并把分页信息转换为json 字符串传送出去
        Page pg = (Page)list;
        int pages = pg.getPages();
        Map<String,Object> rs = new HashMap<>();
        rs.put("list", list);
        rs.put("pages", pages);
        rs.put("page", page);
        return rs;

    }

    //保存学员信息
    @RequestMapping("/save")
    public Student save(Student bean){
        if(service.save(bean)){
            return bean;
        }else{
            return null;
        }

    }

    //删除学员信息
    @RequestMapping("/remove/{stuId}")
    public boolean remove(@PathVariable("stuId") Integer stuId){
        return service.remove(stuId);
    }

    //查找某个学员
    @RequestMapping("/find/{stuId}")
    public Student find(@PathVariable("stuId") Integer stuId){
        return service.find(stuId);
    }

    //查找所有学员不分页
    @RequestMapping("/findAll")
    public List<Student> findAll(){
        return service.queryAll();
    }


    //修改学员状态
    @RequestMapping("enable/{stuId}/{status}")
    public boolean enable(@PathVariable("stuId") Integer stuId,@PathVariable("status") Integer status){
        return service.enable(stuId,status);

    }

    //查询预约信息，包含分页，条件查询，查询当前日期以后的信息是默认条件，教练id，加了id 和科目id
    @RequestMapping("/queryreverse")
    public Map<String,Object> queryReverse(HttpServletRequest request){
        //查询条件，分别是  科目id，学员id，课程id，和今日预约
        String subId = request.getParameter("subId");
        Map<String,Object> map = new HashMap<>();
        //科目id条件查询
        if(subId!=null && !subId.equals("")){
            map.put("subId",Integer.parseInt(subId));
        }else{
            map.put("subId",null);
        }
        //学员id条件查询不考虑评论
        String stuId = request.getParameter("stuId");
        if(stuId!=null && !stuId.equals("")){
            map.put("stuId",Integer.parseInt(stuId));
        }else{
            map.put("stuId",null);
        }
        //教练id条件查询
        String coaId = request.getParameter("coaId");
        if(coaId!=null && !coaId.equals("")){
            map.put("coaId",Integer.parseInt(coaId));
        }else{
            map.put("coaId",null);
        }
        //预约id 条件查询
        String id = request.getParameter("id");
        if(id!=null && !id.equals("")){
            map.put("id",Integer.parseInt(id));
        }else{
            map.put("id",null);
        }
        String type = request.getParameter("type");
        String resType = request.getParameter("resType");
        String comType = request.getParameter("comType");
        //今天及以后的日期
        if(type!=null && !type.equals("")){
            Date teaTime = new Date();
            java.sql.Date sqlDate = new java.sql.Date(teaTime.getTime());
            map.put("teaTime",sqlDate);
            resType = null;
            comType = null;
        }else{
            map.put("teaTime",null);
        }
        //当前信息，也就是今天预约信息
        if(resType!=null && !resType.equals("")){
            Date teaTime = new Date();
            java.sql.Date sqlDate = new java.sql.Date(teaTime.getTime());
            map.put("resType",sqlDate);
        }else{
            map.put("resType",null);
        }
        //过去信息，过去预约信息
        if(comType!=null && !comType.equals("")){
            Date teaTime = new Date();
            java.sql.Date sqlDate = new java.sql.Date(teaTime.getTime());
            map.put("comType",sqlDate);
        }else{
            map.put("comType",null);
        }
        //评论信息加预约信息
        String commentType = request.getParameter("commentType");
        if(commentType!=null && !commentType.equals("")){
            map.put("commentType",commentType);
        }else{
            map.put("commentType",null);
        }
        //个人预约信息加评论信息 已评论
        String wxCommentType = request.getParameter("wxCommentType");
        if(wxCommentType!=null && !wxCommentType.equals("")){
            map.put("wxCommentType",Integer.parseInt(wxCommentType));
        }else{
            map.put("wxCommentType",null);
        }
        //个人预约信息加评论信息 微评论
        String waitCommentType = request.getParameter("waitCommentType");
        if(waitCommentType!=null && !waitCommentType.equals("")){
            map.put("waitCommentType",Integer.parseInt(waitCommentType));
        }else{
            map.put("waitCommentType",null);
        }
        //设置分页条件
        int page=1;
        int rows=5;
        String p = request.getParameter("page");
        if(p!=null && !p.equals("")){
            page = Integer.parseInt(p);
        }
        String r = request.getParameter("rows");
        if(r!=null && !r.equals("")){
            rows = Integer.parseInt(r);
        }
        //分页插件的工具类 ，传入当前页数和条数，并且先调用在查询
        PageHelper.startPage(page,rows);
        //查询出来的教练信息
        List<Student> list = service.queryReverse(map);
        //获取分页信息，并且一并把分页信息转换为json 字符串传送出去
        Page pg = (Page)list;
        int pages = pg.getPages();
        Map<String,Object> rs = new HashMap<>();
        rs.put("list", list);
        rs.put("pages", pages);
        rs.put("page", page);
        return rs;

    }

    //查询所有学员所有预约信息
    @RequestMapping("/queryAllRes")
    public List<Student> queryAllRes(){
        return service.queryAllRes();
    }

    //查询一个人的预约信息 评论用
    @RequestMapping("/queryOneRes/{id}")
    public Student queryOneRes(@PathVariable("id")Integer id){
        return service.queryReverse2(id);
    }

    //学员登陆
    @RequestMapping(value ="/login",method= RequestMethod.POST)
    public Student login(String username, String password){
        Student bean = new Student();
        bean.setNumber(username);
        bean.setPassword(password);
        bean = service.login(bean);
        return bean;
    }

    //查询最大学员数
    @RequestMapping("/maxId")
    public Integer querymaxId(){
        return service.maxId();
    }

    //查询某个学员是否已经预约此课程
    @RequestMapping("/queryPeo/{stuId}/{classId}")
    public Student queryPeo(@PathVariable("stuId") Integer stuId,@PathVariable("classId") Integer classId){
        return this.service.queryPeo(stuId,classId);
    }

    //查看学时学时情况
    @RequestMapping("/queryPeriod")
    public Map<String,Object> queryPeriod(HttpServletRequest request){
        //查询条件
        String stuId = request.getParameter("stuId");
        Map<String,Object> map = new HashMap<>();
        if(stuId!=null && !stuId.equals("")){
            map.put("stuId",Integer.parseInt(stuId));
        }else{
            map.put("stuId",null);
        }
        String subId = request.getParameter("subId");
        if(subId!=null && !subId.equals("")){
            map.put("subId",Integer.parseInt(subId));
        }else{
            map.put("subId",null);
        }
        //设置分页条件
        int page=1;
        int rows=8;
        String p = request.getParameter("page");
        if(p!=null && !p.equals("")){
            page = Integer.parseInt(p);
        }
        String r = request.getParameter("rows");
        if(r!=null && !r.equals("")){
            rows = Integer.parseInt(r);
        }
        //分页插件的工具类 ，传入当前页数和条数，并且先调用在查询
        PageHelper.startPage(page,rows);
        List<Student> list = service.queryPeriod(map);
        //获取分页信息，并且一并把分页信息转换为json 字符串传送出去
        Page pg = (Page)list;
        int pages = pg.getPages();
        Map<String,Object> rs = new HashMap<>();
        rs.put("list", list);
        rs.put("pages", pages);
        rs.put("page", page);
        return rs;
    }

    //查询这个学生有没有预约该时间对应场次
    @RequestMapping("/querySess")
    public Student querySess(HttpServletRequest request){
        //查询条件，分别是  科目id，学员id，课程id，和今日预约
        Map<String,Object> map = new HashMap<>();
        String stuId = request.getParameter("stuId");
        if(stuId!=null && !stuId.equals("")){
            map.put("stuId",Integer.parseInt(stuId));
        }else{
            map.put("stuId",null);
        }
        String teaTime = request.getParameter("teaTime");
        if(teaTime!=null && !teaTime.equals("")){
            map.put("teaTime",teaTime);
        }else{
            map.put("teaTime",null);
        }
        String classSess = request.getParameter("classSess");
        //今天及以后的日期
        if(classSess!=null && !classSess.equals("")){
            map.put("classSess",classSess);
        }else{
            map.put("classSess",null);
        }
        return service.querySess(map);

    }


}
